﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class aboutme
    Inherits cacheProcessor

    Public Const _pathidentity As String = "/aboutme"
    Protected WithEvents aboutmeconfig As configMonitor = Nothing

    Protected Overrides Function retiredticks() As Long
        'manually retire
        Return -1
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial()
        Dim configfile As String = Nothing
        configfile = config.parameter(configNaming.aboutme.name, configNaming.aboutme.configfile _
                                      , configNaming.defaultValue.aboutme.configfile)
        aboutmeconfig = New configMonitor()
        assert(aboutmeconfig.open(configfile), "cannot open configfile " + configfile)
    End Sub

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function pathidentitylen() As Long
        Return strlen(pathidentity())
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.title().text = applicationSign() + " - aboutme"
        page.body().id = "id_aboutme"
        page.addcsslink(csslinkhref)

        Dim img As img = Nothing
        img = New img(buff)
        img.width = "100%"
        img.src = aboutmeconfig.parameter(configNaming.aboutme.name, configNaming.aboutme.image _
                                          , configNaming.defaultValue.aboutme.image)
        page.body().addchild(img)

        Dim label As label = Nothing
        label = New label(buff)
        label.text = aboutmeconfig.parameter(configNaming.aboutme.name, configNaming.aboutme.description _
                                             , configNaming.defaultValue.aboutme.description)
        page.body().addchild(label)

        Dim links As vector(Of String) = Nothing
        Dim titles As vector(Of String) = Nothing
        Dim images As vector(Of String) = Nothing
        If aboutmeconfig.readlist(configNaming.aboutme.name, configNaming.aboutme.link, links) _
            AndAlso aboutmeconfig.readlist(configNaming.aboutme.name, configNaming.aboutme.title, titles) _
            AndAlso aboutmeconfig.readlist(configNaming.aboutme.name, configNaming.aboutme.linkimage, images) Then
            Dim linkset As linkset = Nothing
            linkset = New linkset(buff)
            linkset.setdata(links, titles, images, defaultLinkImage)
            page.body().addchild(linkset)
        End If

        If page.render() Then
            Return True
        Else
            raiseError("cannot render xhtmlpage, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Private Sub aboutmeconfig_ConfigChanged() Handles aboutmeconfig.ConfigChanged
        cache.clear()
    End Sub
End Class
